﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace eipch
{
    class IPAddress
    {
        private String ipString;
        private int firstSegment, secondSegment, thirdSegment, fourthSegment;

        public IPAddress()
        {
            firstSegment = 0;
            secondSegment = 0;
            thirdSegment = 0;
            fourthSegment = 0;
        }

        public IPAddress(String ip)
        {
            Int16 firstSegment = 0;
            Int16 secondSegment = 0;
            Int16 thirdSegment = 0;
            Int16 fourthSegment = 0;

            try
            {
                String[] ipArray = ip.Split('.');
                if (ipArray.Length != 4) throw new IPAddressException("Falsche Anzahl an Segmenten");

                firstSegment = Int16.Parse(ipArray[0]);
                secondSegment = Int16.Parse(ipArray[1]);
                thirdSegment = Int16.Parse(ipArray[2]);
                fourthSegment = Int16.Parse(ipArray[3]);

                if (firstSegment < 0 || firstSegment > 255) throw new IPAddressException("Segment muss zwischen 0 und 255 sein");
                if (secondSegment < 0 || secondSegment > 255) throw new IPAddressException("Segment muss zwischen 0 und 255 sein");
                if (thirdSegment < 0 || thirdSegment > 255) throw new IPAddressException("Segment muss zwischen 0 und 255 sein");
                if (fourthSegment < 0 || fourthSegment > 255) throw new IPAddressException("Segment muss zwischen 0 und 255 sein");
            }
            catch (IPAddressException)
            {
                MessageBox.Show("Fehlerhafte IP-Adress-Eingabe", "Fehler");
            }
            catch (FormatException)
            {
                MessageBox.Show("Fehlerhafte IP-Adress-Eingabe", "Fehler");
            }

            this.firstSegment = firstSegment;
            this.secondSegment = secondSegment;
            this.thirdSegment = thirdSegment;
            this.fourthSegment = fourthSegment;
        }

        public void setFirstSegment(int segment)
        {
            if (segment < 0 || segment > 255) throw new IPAddressException("Segment muss zwischen 0 und 255 sein");
            firstSegment = segment;
        }

        public void setSecondSegment(int segment)
        {
            if (segment < 0 || segment > 255) throw new IPAddressException("Segment muss zwischen 0 und 255 sein");
            secondSegment = segment;
        }

        public void setThirdSegment(int segment)
        {
            if (segment < 0 || segment > 255) throw new IPAddressException("Segment muss zwischen 0 und 255 sein");
            thirdSegment = segment;
        }

        public void setFourthSegment(int segment)
        {
            if (segment < 0 || segment > 255) throw new IPAddressException("Segment muss zwischen 0 und 255 sein");
            fourthSegment = segment;
        }

        private void generateIpString()
        {
            ipString = String.Format("{0}.{1}.{2}.{3}", firstSegment, secondSegment, thirdSegment, fourthSegment);
        }

        public static List<IPAddress> generateIPAddressList(String[] stringArray)
        {
            List<IPAddress> ipAddressList = new List<IPAddress>();
            foreach (String ip in stringArray)
            {
                ipAddressList.Add(new IPAddress(ip));
            }
            return ipAddressList;
        }

        public override string ToString()
        {
            generateIpString();
            return ipString;
        }
    }
}
